#region Copyright

//  Weapsy - http://www.weapsy.org
//  Copyright (c) 2011-2012 Luca Cammarata Briguglia
//  Licensed under the Weapsy Public License Version 1.0 (WPL-1.0)
//  A copy of this license may be found at http://www.weapsy.org/Documentation/License.txt

#endregion

using System;
using System.Collections.Generic;
using System.Web;
using System.Runtime.CompilerServices;
using System.Web.Caching;
using System.Collections;
using System.Text.RegularExpressions;

namespace Infrastructure.Crosscutting.Caches
{
    public class CacheService : ICacheService
    {
        public void Insert(string key, object obj, double timeOut, CacheItemPriority priority)
        {
            Insert(key, obj, null, timeOut, TimeSpan.Zero, priority, null);
        }

        public void Insert(string key, object obj, CacheDependency dependency, double timeOut, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback removedCallback)
        {
            if ((obj != null))
            {
                Cache cache = HttpRuntime.Cache;
                if (cache[key] == null)
                {
                    cache.Insert(key, RuntimeHelpers.GetObjectValue(obj), dependency, DateTime.Now.AddSeconds(timeOut), slidingExpiration, priority, removedCallback);
                }
            }
        }

        public object Get(string key)
        {
            Cache cache = HttpRuntime.Cache;
            return cache[key];
        }

        public void Remove(string key)
        {
            Cache cache = HttpRuntime.Cache;
            if (cache[key] != null)
            {
                cache.Remove(key);
            }
        }

        public void RemoveByPattern(string pattern)
        {
            Cache Cache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = Cache.GetEnumerator();
            Regex rgx = new Regex(pattern, (RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase)));
            while (enumerator.MoveNext())
            {
                if (rgx.IsMatch(enumerator.Key.ToString()))
                {
                    Cache.Remove(enumerator.Key.ToString());
                }
            }
        }

        public void Clear()
        {
            Cache cache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                cache.Remove(enumerator.Key.ToString());
            }
        }

        /// <summary>
        /// Caches the item priorities.
        /// </summary>
        /// <returns></returns>
        public Dictionary<byte, string> CacheItemPriorities()
        {
            Dictionary<byte, string> dic = new Dictionary<byte, string>();
            dic.Add((byte)CacheItemPriority.Low, CacheItemPriority.Low.ToString());
            dic.Add((byte)CacheItemPriority.BelowNormal, CacheItemPriority.BelowNormal.ToString());
            dic.Add((byte)CacheItemPriority.Normal, CacheItemPriority.Normal.ToString());
            dic.Add((byte)CacheItemPriority.AboveNormal, CacheItemPriority.AboveNormal.ToString());
            dic.Add((byte)CacheItemPriority.High, CacheItemPriority.High.ToString());
            dic.Add((byte)CacheItemPriority.NotRemovable, CacheItemPriority.NotRemovable.ToString());
            return dic;
        }
    }

}